package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanhua.dubbo.api.db.TestSoulApi;
import com.tanhua.dubbo.api.mapper.*;
import com.tanhua.model.db.*;
import com.tanhua.model.db.TestCharacter;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;

@DubboService
public class TestSoulApiImpl implements TestSoulApi {

    @Autowired
    private TestSoulMapper testSoulMapper;

    @Autowired
    private QuestionsMapper questionsMapper;

    @Autowired
    private OptionsMapper optionsMapper;

    @Autowired
    private CharacterMapper characterMapper;

    @Autowired
    private TestResultMapper testResultMapper;

    @Autowired
    private QuestionAndoptionsScoreMapper questionAndoptionsScoreMapper;

    @Override
    public List<TestSoul> getTestSoul() {
        List<TestSoul> testSouls = testSoulMapper.selectList(null);
        return testSouls;
    }

    @Override
    public List<Questions> getQuestions(Integer id) {
        LambdaQueryWrapper<Questions> qw = new LambdaQueryWrapper<>();
        qw.eq(Questions::getTestSoulId,id);
        List<Questions> questionsList = questionsMapper.selectList(qw);
        return questionsList;
    }
    @Override
    public List<Options> getOptions(Integer id) {
        LambdaQueryWrapper<Options> qw = new LambdaQueryWrapper<>();
        qw.eq(Options::getQuestionsId,id);
        List<Options> options = optionsMapper.selectList(qw);
        return options;
    }

    /**
     *      查询性格
     * @param id
     * @return
     */
    @Override
    public TestCharacter getCharacter(int id) {
        LambdaQueryWrapper<TestCharacter> qw = new LambdaQueryWrapper<>();
        qw.eq(TestCharacter::getScore,id);
        TestCharacter character = characterMapper.selectOne(qw);
        return character;
    }

    /**
     *      查询题目
     * @param questionId
     * @return
     */
    @Override
    public List<Questions> getQuestionByIds(List<Integer> questionId) {
        LambdaQueryWrapper<Questions> qw = new LambdaQueryWrapper<>();
        qw.in(Questions::getId,questionId);
        List<Questions> questionsList = questionsMapper.selectList(qw);
        return questionsList;
    }



    /**
     *          将用户测试结果存入数据库
     * @param result
     * @param testId
     * @param userId
     * @return
     */
    @Override
    public Integer addResult(TestResult result,Long testId,Long userId) {
        LambdaQueryWrapper<TestResult> qw = new LambdaQueryWrapper<>();
        qw.eq(TestResult::getTestSoulId,testId).eq(TestResult::getUserid,userId);
        Integer integer = testResultMapper.selectCount(qw);
        if (integer == 0) {
            result.setCreated(System.currentTimeMillis());
            testResultMapper.insert(result);
        }else {
            testResultMapper.update(result,qw);
            TestResult testResult = testResultMapper.selectOne(qw);
            return testResult.getId();
        }
        return result.getId();
    }

    /**
     *      获取用户测试结果
     * @param id
     * @param userId
     * @return
     */
    @Override
    public TestResult getTestResult(Integer id,Long userId) {
        LambdaQueryWrapper<TestResult> qw = new LambdaQueryWrapper<>();
        qw.eq(TestResult::getId,id);
        TestResult result = testResultMapper.selectOne(qw);
        return result;
    }



    /**
     *      获取用户分数
     * @param optionId
     * @return
     */
    @Override
    public List<QuestionAndoptionsScore> sumByUserScore(List<Integer> questionId,List<Integer> optionId) {

        return questionAndoptionsScoreMapper.sumScore(questionId,optionId);
    }

    /**
     *      获取相似用户id
     * @param userScore
     * @return
     */
    @Override
    public List<Integer> getUserIdByScore(Integer userScore,Long userId) {
        LambdaQueryWrapper<TestResult> lqw = new LambdaQueryWrapper<>();
        lqw.le(TestResult::getUserScore,userScore + 5);
        lqw.ge(TestResult::getUserScore,userScore - 5);
        lqw.ne(TestResult::getUserid,userId);
        List<TestResult> testResultList = testResultMapper.selectList(lqw);
        List<Integer> userIds = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(testResultList)) {
            userIds = testResultList.stream().map(TestResult::getUserid).collect(Collectors.toList());
        }
        return userIds;
    }


}
